import tkinter as tk
# 使用对话框
from tkinter import messagebox

# 对JSON数据进行分级处理
def levels(js):
    i = 1
    len_js = len(js)
    # 整数除法，取中间数
    mid = (len_js // 2) + 1
    arr = []
    up = {}
    down = {}
    for a in js:
        i += 1
        if i < mid:
            up[a] = js[a]
        elif i == mid:
            arr.append(js[a])
        else:
            down[a] = js[a]
    # 上等
    ii = 1
    len_up = len(up)
    mid_up = len_up // 2
    for aa in up:
        ii += 1
        if ii > mid_up:
            arr.insert(0, up[aa])
            break
    # 下等
    iii = 1
    len_down = len(down)
    mid_down = len_down // 2
    for aaa in down:
        iii += 1
        if iii >= mid_down:
            arr.append(down[aaa])
            break
    return arr

# 等级区间划分
def level():
    # 姓名
    nm = entry_name.get()
    a = levels(DATA)
    n = DATA.get(nm, 0)
    s = ''
    if n >= a[0]:
        s = '甲等'
    elif n < a[0] and n >= a[1]:
        s = '乙等'
    elif n < a[1] and n >= a[2]:
        s = '丙等'
    else:
        s = '丁等'
    messagebox.showinfo('等级', f'{nm} 积分 {n}，属于 {s}')

# 运动员对应的积分，注意这个积分是有序的
DATA = {
    '张一': 100, '张二': 99, '张三': 95, '张四': 94, '张五': 90, '张六': 90, '张七': 89, '张八': 88,
    '张九': 85, '张十': 80, '张十一': 80, '张十二': 78, '张十三': 77, '张十四': 70, '张十五': 60
}

# 创建主窗口
root = tk.Tk()
root.title('动态判断与会运动员的等级')

# 创建标签和输入框
tk.Label(root, text='运动员姓名：').pack(side="left")
entry_name = tk.Entry(root)
entry_name.insert(0,"张三")
entry_name.pack(side="left")
button_level = tk.Button(root, text='等级', command=level)
button_level.pack(side="left")

# 运行主循环
root.mainloop()